<?php

namespace App\Http\Controllers;

use App\Entity\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class EmailController extends Controller
{

    public function verify($token)
    {
        //验证token，根据token查出用户信息并更新
        $user = User::where('confirmation_token',$token)->first();
        if (is_null($user)){
            flash('邮箱验证失败！')->error()->important();
            return redirect('/');
        }

        //更新用户逻辑
        $user->is_active=1;
        $user->confirmation_token=str_random(40);
        $user->save();
        Auth::login($user);
        flash('邮箱验证成功！')->success()->important();
        return redirect('/home');
    }
}
